package com.youfan.book.controller;

import com.youfan.book.domain.Admin;
import com.youfan.book.service.AdminService;
import com.youfan.book.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("admin")
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private LibraryService libraryService;

    @GetMapping("manager")
    public String manager(Model model) {
        model.addAttribute("adminList", adminService.list());
        System.out.println(model.getAttribute("adminList"));
        return "/admin/manager";
    }

    @GetMapping("manager_add")
    public String managerAdd() {
        return "/admin/manager_add";
    }

    @GetMapping("manager_delete")
    public String managerDelete(Long id) {
        adminService.removeById(id);
        return "redirect:/admin/manager";
    }

    @GetMapping("manager_modify")
    public String managerModify(Long id, Model model) {
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return "/admin/manager_Modify";
    }

    @PostMapping("manager_modify_do")
    public String managerModifyDo(Admin admin) {
        adminService.updateById(admin);
        return "redirect:/admin/manager";
    }

    @PostMapping("manager_add_do")
    public String managerAddDo(Admin admin) {
        adminService.save(admin);
        return "redirect:/admin/manager";
    }

    @GetMapping("home")
    public String adminHome(Model model) {
        model.addAttribute("library",libraryService.getById(1));
        return "/admin/home";
    }

}
